<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.0/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .chat-window {
            background-color: #f0f0f0;
            border: 1px solid #ccc;
            height: 80vh;
            width: 100%;
        }
        .chat-window .chat-content {
            height: 100%;
            overflow-y: auto;
        }
        .chat-window .chat-input {
            height: 50px;
            width: 100%;
        }
    </style>
</head>
<body>
<div class="col-12">
     <div class="chat-window h-30 w-30">
            <div class="chat-content"></div>
     </div>
     <label for="input-chat"><input type="text" id="input-chat"/></label>
    <button  type="button" class="btn-primary d-block ">发送消息</button>
</div>


<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.7.1/jquery.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.0/js/bootstrap.js"></script>
<script>
    var ws = new WebSocket('ws://localhost:3000');
    ws.onopen = function () {
      console.log('连接成功');
    };
    ws.onmessage = function (e) {
      console.log('收到消息：' + e.data);
      $('.chat-window .chat-content').append('<p>' + e.data + '</p>');
    };
    ws.onclose = function () {
      console.log('连接关闭');
    };
    ws.onerror = function () {
      console.log('出现错误');
    };
   $('button').on('click', function () {
         const input= $('#input-chat');
         const message = input.val();
         ws.send(message);
         input.val('');
   })
</script>
</body>
</html>